Ext.onReady(function(){
	var itemRecord = Ext.data.Record.create([
		{name:'id',mapping:'itemId'},
		{name:'mc',mapping:'itemName'}
	]);
	
	var rootReader = new Ext.data.JsonReader(
		{
			root:'roots',
			id:'itemId'
		},itemRecord);

	var parentStore = new Ext.data.Store({
		proxy:new Ext.data.HttpProxy({
			url:'edit/loadRoot.wkm'
		}),
		reader:rootReader
	});
	parentStore.load();
	
	var childReader = new Ext.data.JsonReader(
		{
			root:'roots',
			id:'itemId'
		},itemRecord);
	
	var childStore = new Ext.data.Store({
		reader:childReader
	});
	
	var store=new Ext.data.JsonStore({
		url: 'edit/loadBodys.wkm',
    	root: 'roots',
    	fields: [
    		{name:'itemName',type:'string'},
    		{name:'itemDesc',type:'string'},
    		{name:'itemId',type:'string'},
    		{name:'itemParent',type:'string'},
    		{name:'itemSeq',type:'string'},
    		{name:'target',type:'string'}
    	]
	});
	
	store.load();
	
	var comboPanel = new Ext.Panel({
		frame:true,
		renderTo:'combo',
		layout:'column',
		height:40,
		id:'comboPanel',
		items:[
			{
				fieldLabel:'请选择分类',
				xtype:'combo',
				store:parentStore,
				valueField:'id',
				displayField:'mc',
				emptyText:'请选择分类',
				editable:true,
				hiddenName:'interviewsDetail.parent_category',
				triggerAction:'all',
				id:'parent_id',
				name:'parent',
				autoShow:true,
				width:100,
				height:40,
				listeners:{
					select:function(mycombox,record,index){
						childStore.proxy = new Ext.data.HttpProxy({
							url:'edit/loadChildren.wkm?itemId=' + mycombox.value
						});
						childStore.load();
					}
				}
			},{
				xtype:'combo',
				store:childStore,
				valueField:'id',
				displayField:'mc',
				emptyText:'请选择分类',
				editable:true,
				hiddenName:'interviewsDetail.parent_category',
				triggerAction:'all',
				id:'child_id',
				name:'child',
				autoShow:true,
				width:100,
				height:40,
				listeners:{
					select:function(mycombox,record,index){
						//childStore.proxy = new Ext.data.HttpProxy({
						//	url:'loadChild.wkm?itemParent=' + combo.value
						//});
						//childStore.load();
						var comvalue = mycombox.value;
						store.load({
							url: 'edit/loadBodys.wkm',
							params:{'itemParent':comvalue}
						});
						//Ext.Msg.show({
						//   title:'信息提示',
						//   msg: '你确定选择了' + mycombox.value,
						//   buttons: Ext.Msg.YESNOCANCEL,
						//   animEl: 'elId',
						//   icon: Ext.MessageBox.QUESTION
						//});
					}
				}
			}
		]
	});
	
	//setTimeout(function(){},3000);
	
	var checkbox = new Ext.grid.CheckboxSelectionModel();
	var columns = new Ext.grid.ColumnModel([
		checkbox,
		{header:"序号",width:200,dataIndex:"itemId",sortable:true,editor:new Ext.form.Label()},
		{header:"上级序号",dataIndex:"itemParent",sortable:true,editor:new Ext.form.Label()},
		{header:"名称",dataIndex:"itemName",sortable:true,editor:new Ext.form.TextField()},
		{header:"地址",dataIndex:"target",sortable:true,editor:new Ext.form.TextField()},
		{header:"顺序",dataIndex:"itemSeq",sortable:true,editor:new Ext.form.TextField()},
		{header:"描述",dataIndex:"itemDesc",sortable:true,editor:new Ext.form.TextField()}
	]);
	
	var grid = new Ext.grid.EditorGridPanel({
		renderTo:'tables',
		title:'菜单列表编辑',
		cm:columns,
		selModel:checkbox,
		autoHeight:true,
		store:store,
		tbar:[
			{
				text:'导航栏',
				cls:'x-btn-text-icon',
				handler:function(){
					location.href='home/index.wkm';
				}
				
			},{
				text:'保存',
				cls:'x-btn-text-icon',
				handler:function(){
					var store = grid.getStore();
					var modified = store.modified.slice(0);
					var jsonArray = [];
					var itemId = '';
					var itemName = '';
					var itemDesc = '';
					var target = '';
					Ext.each(modified,function(m){
						//alert(m.data.ADDRESS);//读取当前被修改的记录的地址  
						//m.data中保存的是当前Recored的所有字段的值json，不包含结构信息  
						jsonArray.push(m.data);
						itemId = m.get('itemId');
						itemName = m.get('itemName');
						itemDesc = m.get('itemDesc');
						target = m.get('target');
					});
					if(jsonArray.length === 0){
						Ext.Msg.alert('信息提示框','没有对表格进行修改!');
						return ;
					}
					Ext.Msg.confirm('信息提示框','真的要保存修改行吗?',function(btn){
						if(btn == 'yes'){
							Ext.Ajax.request({
								method:'POST',
								url:'edit/updateBody.wkm',
								success:function(response,options){
									Ext.Msg.alert('信息提示框','修改成功!');
								},
								failure:function(response,options){
									Ext.Msg.alert('信息提示框',Ext.util.JSON.encode(jsonArray));
								},
								//jsonData:Ext.util.JSON.encode(jsonArray),
								params:{'itemId':itemId,'itemName':itemName,'itemDesc':itemDesc,'target':target}
							});
						}
					});
					//Ext.Msg.alert('信息提示框',jsonArray.length);
				}
			},{
				text:'删除',
				cls:'x-btn-text-icon',
				handler:function(){
					var selectModel = grid.getSelectionModel();
					if(selectModel.hasSelection()){
						Ext.Msg.confirm('信息提示框','真的要删除选中行吗?',function(btn){
							if(btn == 'yes'){
								var record = selectModel.getSelected();//获取单元格的行列
								var obj = record.get('itemId');
								Ext.Ajax.request({
									method:'POST',
									url:'edit/deleteBody.wkm',
									success:function(response,options){
										Ext.Msg.alert('信息提示框','删除成功!');
									},
									failure:function(response,options){
										Ext.Msg.alert('信息提示框','删除失败!');
									},
									params:{'itemId':obj}
								});
								grid.getStore().remove(record);
							} else {
								Ext.Msg.alert('信息提示框','已经取消!');
							}
						});
					}
					//Ext.Msg.alert("信息提示框","请先选中要删除的行!");
				}
			}
		]
	});
});